//
//  ROFoodList.swift
//  NeusoftSchool
//
//  Created by 李狗蛋 on 2017/4/5.
//  Copyright © 2017年 李狗蛋. All rights reserved.
//

import UIKit

class ROFoodList: UITableViewController {
    
    var shop: ROShopModel!{ //商店模型
        didSet(oldValue){
            shopId = shop.shop_id
            shopName = shop.shopname
        }
    }
    var shopId: NSNumber!
    var shopName: NSString!
    var foodList: NSArray = NSArray()
    var collectionBarBtnItem: UIBarButtonItem! //右侧按钮
    var isCollect: NSString! // 查看是否收藏
    var rightBtn: UIButton = UIButton.init()

    override func viewDidLoad() {
        
        super.viewDidLoad()
        self.navigationController?.isNavigationBarHidden = false
        //右侧导航按钮。收藏店铺
        rightBtn.sizeToFit()
        rightBtn.addTarget(self, action :#selector(collectionShop), for: UIControlEvents.touchUpInside)
        collectionBarBtnItem = UIBarButtonItem.init(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = collectionBarBtnItem
        self.title = self.shopName as String?
        //
        //let parameters: NSDictionary = ["shop_id": shop.shop_id]
        let parameters: NSDictionary = ["shop_id": shopId] as NSDictionary
        RONetworkMngTool.sharedNetwodkMngTool().RONetwork_GetFoodListParameters(parameters, view: self.view) { (foodList: NSArray) in
            self.foodList = foodList
            self.tableView.reloadData()
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        
        isCollected()
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }


    override func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.foodList.count == 0 {
            return 0
        }
        return self.foodList.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "foodCell", for: indexPath)

        if self.foodList.count != 0 {
            let food: ROFoodModel = self.foodList[(indexPath as NSIndexPath).row] as! ROFoodModel
            cell.textLabel!.text = food.foodname as String
            //使用afnetworking中的uiimageview家在网络图片
            let imageStr = food.pic as String
            let imageUrl = URL(string: imageStr)
            cell.imageView!.setImageWith(imageUrl, placeholderImage: UIImage(named: "imageViewHolder"))
            cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        }
        return cell
    }
    //
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let currentFood: ROFoodModel = foodList[(indexPath as NSIndexPath).row] as! ROFoodModel
        self.performSegue(withIdentifier: "tofoodDetail", sender: currentFood)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        let foodDetailController: ROFoodDetail = segue.destination as! ROFoodDetail
//        let foodDic = ["phonenum":shop.phonenum,"food":sender as! ROFoodModel] as [String : Any]
//        foodDetailController.foodDic = foodDic as NSDictionary
        foodDetailController.food = sender as! ROFoodModel
//        foodDetailController.shopModel = self.shop
    }
    //收藏店铺
    func collectionShop (){
        let parameters2: NSDictionary = ["user_id": userId,"shop_id": self.shop.shop_id] as NSDictionary
        RONetworkMngTool.sharedNetwodkMngTool().RONetwork_collectShopWithParameters(parameters2, view: self.view) { (success) in
            if success == "1" {
                self.isCollected()
            } else {
                
            }
        }
    }
    //判断之前是否收藏
    func isCollected() {
        
//        let parameters: NSDictionary = ["user_id": userId,"shop_food_id": shop.shop_id,"flag": 0] as NSDictionary
        let parameters: NSDictionary = ["user_id": userId,"shop_food_id": self.shopId,"flag": 0] as NSDictionary
        RONetworkMngTool.sharedNetwodkMngTool().RONetwork_isCollect(parameters, view: self.view) { (isCollect) in
            self.isCollect = isCollect
            
            if self.isCollect == "0" {
                self.rightBtn.setImage(UIImage.init(named: "no.png"), for: UIControlState.normal)
            }else if self.isCollect == "1" {
                self.rightBtn.setImage(UIImage.init(named: "yes.png"), for: UIControlState.normal)
            }
        }
    }
    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
